package com.taokatao.cardmee.business.carddetail;

import android.content.Context;
import android.content.Intent;
import android.text.TextUtils;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;


import com.bumptech.glide.Glide;
import com.taokatao.cardmee.R;
import com.taokatao.cardmee.Utils;
import com.taokatao.cardmee.WebActivity;
import com.taokatao.cardmee.api.model.CardDetailInfo;

import butterknife.BindView;
import butterknife.ButterKnife;

public class CardDetailSubPromotionLayout extends LinearLayout implements View.OnClickListener {
    @BindView(R.id.i_promotion_icon) ImageView iPromotionIcon;
    @BindView(R.id.t_promotion_title) TextView tTitle;
    @BindView(R.id.t_promotion_desc) TextView tDesc;
    @BindView(R.id.i_promotion_url) ImageView iUrl;

    @BindView(R.id.l_content) View lContent;

    private CardDetailInfo.Promotion p;

    public CardDetailSubPromotionLayout(Context context) {
        this(context, null);
    }

    public CardDetailSubPromotionLayout(Context context, AttributeSet attrs) {
        super(context, attrs);
        ButterKnife.bind(this, LayoutInflater.from(context).inflate(R.layout.layout_card_detail_sub_promotion, this));
    }

    public void setData(CardDetailInfo.Promotion data) {
        if (data == null) return;
        p = data;
        tTitle.setText(p.bannerName);
        tDesc.setText(p.bannerDec);
        if (TextUtils.isEmpty(p.bannerUrl)) {
            iUrl.setVisibility(View.GONE);
            lContent.setOnClickListener(null);
        } else {
            iUrl.setVisibility(View.VISIBLE);
            lContent.setOnClickListener(this);
        }

        if (!TextUtils.isEmpty(p.bannerImg)) {
            Glide.with(getContext()).load(p.bannerImg).into(iPromotionIcon);
        }
    }

    @Override
    public void onClick(View v) {
        if (v == lContent) {
            if (p != null) {
                Utils.onMobEvent(getContext(), "clickLinkOfDetail");
                Intent intent = new Intent(getContext(), WebActivity.class);
                intent.putExtra("extra_url", p.bannerUrl);
                getContext().startActivity(intent);
            }
        }
    }
}
